package com.cmol.peopledetectapp.admin;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.cmol.net.Network;
import com.cmol.peopledetectapp.R;
import com.cmol.peopledetectapp.json.FeedBack;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Response;

/**
 * 管理员查看用户建议
 *
 * @author 王国良
 */
public class ListSuggestionActivity extends AppCompatActivity {

    /**
     * 布局列表
     */
    private List<BeanSuggestion> data = new ArrayList<>();
    private Intent intent;
    /**
     * testtext
     */
    private String testText = "做得真好";
    /**
     * 通信模块
     */
    private Network network;
    /**
     * 建议列表
     */
    private List<FeedBack> suggestions;

    /**
     * @param savedInstanceState 默认参数
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_suggestion);

        JSONObject json = new JSONObject();
        network = new Network(json, "/admin/viewFeedBack") {
            /**
             * 处理正常响应
             * 200ok
             * @param call 默认参数
             * @param response 响应报文
             */
            @Override
            protected void process(Call call, Response response) {
                String array = null;
                try {
                    final String res = Objects.requireNonNull(response.body()).string().trim();
                    System.out.println("报文" + res);
                    JSONObject body = new JSONObject(res);
                    array = body.getString("feedBackList");
                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
                Gson gson = new Gson();
                Type listType = new TypeToken<ArrayList<FeedBack>>() {
                }.getType();
                suggestions = gson.fromJson(array, listType);
            }

            /**
             * 连接失败
             */
            @Override
            protected void failureWarn() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(ListSuggestionActivity.this, "请求用户建议列表错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        };

        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
//            e.printStackTrace();
        }
        /*
         * 向列表子项中加入要显示的内容
         */
        for (FeedBack suggest : suggestions) {
            BeanSuggestion beanSuggestion = new BeanSuggestion();
            beanSuggestion.setName(suggest.getUserName().toString());
            beanSuggestion.setSuggestion(suggest.getNote());
            data.add(beanSuggestion);
        }

        ListView listView = findViewById(R.id.SuggestionList);
        listView.setAdapter(new MyAdapterSuggestion(data, this));
    }
}